package com.training.admin.web.controller.system;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.training.admin.entity.department.Department;
import com.training.admin.handler.api.DepartmentApiHandler;
import com.training.admin.service.DataService;

/**
 * 
 * @author Rockett
 * 
 */
@Controller
@RequestMapping("/departments")
@MenuSetting("menu-system-management")
public class DepartmentController {
	
	@Autowired
	private DepartmentApiHandler departmentApiHandler;
    
	@Autowired
	@Qualifier("dataService")
	private DataService<Department> departmentService;
	
	
	
	
	
    /**
     * 
     * @param page
     * @return
     */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@MenuSetting("submenu-department-management")
    public ModelAndView list(Page page, HttpServletRequest request) {
		
		ModelAndView modelAndView = new ModelAndView("department/department-list");
		
		int totals = departmentService.count(Department.class);
		
		if(totals > 0) {
			
			List<Department> departments = departmentService.listByPage(Department.class, page.with(totals));
			
			modelAndView.addObject("departments", departments);
			
			modelAndView.addObject("page", page);
		}
		
		modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));
		return modelAndView;
    }

	
	@RequestMapping(value = "/sync", method = RequestMethod.GET)
	 @MenuSetting("submenu-department-management")
	public ModelAndView sync() {
		
		ModelAndView modelAndView = new ModelAndView("department/department-sync");
		
		return modelAndView;
	}
	
    /**
     * 事业部管理表 - 同步
     * 
     * @param search
     * @return
     */
    @RequestMapping(value = "/sync-list", method = RequestMethod.GET)
    @MenuSetting("submenu-department-management")
    public ModelAndView OnSync(Page page) {
        
    	ModelAndView modelAndView = new ModelAndView("department/department-list");
    	
    	 List<Department> departments = departmentApiHandler.list();
    	
    	modelAndView.addObject("departments", departments);
    	
        return modelAndView;
        
    }


}
